package com.databinding.jetpacktesting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProvider;

import com.databinding.jetpacktesting.databinding.ActivityMainBinding;
import com.databinding.jetpacktesting.room.WordEntity;
import com.databinding.jetpacktesting.room.WordViewModel;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    MyViewModel myViewModel;
    ActivityMainBinding mBinding;
//    WordDataBase wordDataBase;
//    WordDao wordDao;
//    LiveData<List<WordEntity>> allWords;
    WordViewModel wordViewModel;
    OtherViewModel otherViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        myViewModel = new ViewModelProvider(this, new SavedStateViewModelFactory(getApplication(), this))
                .get(MyViewModel.class);
        wordViewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication()))
                .get(WordViewModel.class);

        otherViewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication()))
                .get(OtherViewModel.class);
//过时写法
//        myViewModel = ViewModelProviders.of(this, new SavedStateViewModelFactory(getApplication(), this)).get(MyViewModel.class);
        myViewModel.context = getApplicationContext();
        mBinding.setData(myViewModel);
        mBinding.setClicker(new Clicker());
        mBinding.setLifecycleOwner(this);
        LifeObserver lifeObserver = new LifeObserver(myViewModel);
        getLifecycle().addObserver(lifeObserver);
        // Room 运行报错因为主线程的问题
        //写在单例中了
//        wordDataBase = Room.databaseBuilder(this, WordDataBase.class, "word_db")
//                .allowMainThreadQueries() //强制主线程 暂时不处理 后期去掉
//                .build();
//        wordDataBase = WordDataBase.getInstance(this);
//        wordDao = wordDataBase.getWordDao();
//        allWords = wordDao.getAllWordsLife();
        //livedata
        wordViewModel.getAllWordsLive().observe(this, new Observer<List<WordEntity>>() {
            @Override
            public void onChanged(List<WordEntity> wordEntities) {
                String text = "";
                for (WordEntity wordEntity : wordEntities) {
                    text += wordEntity.getId() + ":" + wordEntity.getWord() + ":" + wordEntity.getChinese() + "\n";
                }

                myViewModel.wordDaoText.setValue(text);
            }
        });

    }

    //使用 livedate
    /*private void upDateView() {
        List<WordEntity> list = wordDao.getAllWords();
        String text = "";
        for (WordEntity wordEntity : list) {
            text += wordEntity.getId() + ":" + wordEntity.getWord() + ":" + wordEntity.getChinese()+"\n";
        }

        myViewModel.wordDaoText.setValue(text);


    }*/

    public class Clicker {
        public void insert(View v) {
            Toast.makeText(MainActivity.this, "in", Toast.LENGTH_SHORT).show();
            WordEntity wordEntity1 = new WordEntity("cici", "我");
            WordEntity wordEntity2 = new WordEntity("cici", "还是我");
//            wordDao.insertWords(wordEntity1, wordEntity2);
//            upDateView();
//            new InsertAsyncTask(wordDao).execute(wordEntity1, wordEntity2);
            wordViewModel.insertWords(wordEntity1, wordEntity2);
        }

        public void update(View v) {
            WordEntity wordEntity = new WordEntity("harisucici", "也是我");
            wordEntity.setId(13);
//            wordDao.update(wordEntity);
//            upDateView();
//            new UpdateAsyncTask(wordDao).execute(wordEntity);
            wordViewModel.updateWords(wordEntity);

        }

        public void clear(View v) {
//            wordDao.deletAll();
//            upDateView();
//            new DeleteAllAsyncTask(wordDao).execute();
            wordViewModel.deleteAll();

            Toast.makeText(MainActivity.this, otherViewModel.count+"", Toast.LENGTH_SHORT).show();

        }

        public void delete(View v) {
            WordEntity wordEntity = new WordEntity("harisucici", "也是我");
            wordEntity.setId(13);
//            wordDao.deleteWords(wordEntity);
//            upDateView();
//            new DeleteAsyncTask(wordDao).execute(wordEntity);
            wordViewModel.deleteWords(wordEntity);
            Intent intent = new Intent(MainActivity.this, OtherActivity.class);
            startActivity(intent);
        }

    }


}